package com.caipiao.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;

/**
 * Created by lishuai24 on 2018/8/28.
 */
public class LotteryUtils {

    public static String getPrizeDetail(String details, int half_first, int half_second, int first, int second) {
        if(StringUtils.isEmpty(details)) {
            return "";
        }
        JSONObject detailsObj = JSON.parseObject(details);
        JSONObject result = new JSONObject();
        result.put("detail2", getShengpingfu((JSONObject) detailsObj.get("detail2"), first, second));
        result.put("detail3", getRangShengpingfu((JSONObject) detailsObj.get("detail3"), first, second));
        result.put("detail4", getBifen((JSONObject) detailsObj.get("detail4"), first, second));
        result.put("detail5", getZongjinqiu((JSONObject) detailsObj.get("detail5"), first, second));
        result.put("detail6", getBanquanchang((JSONObject) detailsObj.get("detail6"), half_first, half_second, first, second));
        return result.toJSONString();
    }

    private static JSONObject getShengpingfu(JSONObject detail2Obj, int first, int second) {
        JSONObject shengpingfuObj = new JSONObject();
        String shengfuping = getSheng(first, second);
        shengpingfuObj.put(shengfuping, detail2Obj.getString(shengfuping));
        return shengpingfuObj;
    }

    private static String getSheng(int first, int second) {
        String shengfuping = "";
        if(first > second) {
            shengfuping = "胜";
        } else if(first == second) {
            shengfuping = "平";
        } else {
            shengfuping = "负";
        }
        return shengfuping;
    }

    private static JSONObject getRangShengpingfu(JSONObject detail3Obj, int first, int second) {
        String rang = detail3Obj.getString("让球");
        int num = Integer.parseInt(rang);
        JSONObject shengpingfuObj = new JSONObject();
        String shengfuping = getSheng(first + num, second);
        shengpingfuObj.put("(" + rang + ")" + shengfuping, detail3Obj.getString(shengfuping));
        return shengpingfuObj;
    }

    private static JSONObject getBifen(JSONObject detail4Obj, int first, int second) {
        JSONObject ret = new JSONObject();
        String bifen = first + ":" + second;
        String bifenBet = detail4Obj.getString(bifen);
        if(bifenBet == null) {
            if(first > second) {
                bifen = "胜其他";
            } else if(first == second) {
                bifen = "平其他";
            } else {
                bifen = "负其他";
            }
            bifenBet = detail4Obj.getString(bifen);
        }
        ret.put(bifen, bifenBet);
        return ret;
    }

    private static JSONObject getZongjinqiu(JSONObject detail5Obj, int first, int second) {
        JSONObject ret = new JSONObject();
        String num = "" + (first + second);
        String numBet = detail5Obj.getString(num);
        if(numBet == null) {
            num = "7+";
            numBet = detail5Obj.getString(num);
        }
        ret.put(num, numBet);
        return ret;
    }

    private static JSONObject getBanquanchang(JSONObject detail6Obj, int half_first, int half_second, int first, int second) {
        String key = getSheng(half_first, half_second) + getSheng(first, second);
        JSONObject ret = new JSONObject();
        ret.put(key, detail6Obj.getString(key));
        return ret;
    }

    public static void main(String[] args) {
        String str = "\n" +
                "\n" +
                "{\"detail1\":{\"title1\":\"单场开奖结果\",\"玩法\":\"开奖结果\",\"胜平负\":\"\",\"让球胜平负\":\"(+1)\",\"猜比分\":\"\",\"总进球数\":\"\",\"半全场胜平负\":\"\"},\"detail2\":{\"title2\":\"　　胜平负过关固定奖金\",\"胜\":\"2.53\",\"平\":\"3.40\",\"负\":\"2.13\"},\"detail3\":{\"title3\":\"　　让球胜平负过关固定奖金(所有玩法固定奖金以购买时票面显示奖金为准)\",\"让球\":\"+1\",\"胜\":\"1.49\",\"平\":\"4.00\",\"负\":\"4.20\"},\"detail4\":{\"title4\":\"　　比分固定奖金\",\"1:0\":\"11.00\",\"2:0\":\"15.00\",\"2:1\":\"8.50\",\"3:0\":\"29.00\",\"3:1\":\"18.00\",\"3:2\":\"19.00\",\"4:0\":\"60.00\",\"4:1\":\"39.00\",\"4:2\":\"39.00\",\"5:0\":\"200.0\",\"5:1\":\"120.0\",\"5:2\":\"150.0\",\"胜其他\":\"39.00\",\"0:0\":\"14.00\",\"1:1\":\"7.00\",\"2:2\":\"10.50\",\"3:3\":\"34.00\",\"平其他\":\"150.0\",\"0:1\":\"9.50\",\"0:2\":\"13.00\",\"1:2\":\"7.80\",\"0:3\":\"22.00\",\"1:3\":\"15.00\",\"2:3\":\"17.00\",\"0:4\":\"50.00\",\"1:4\":\"34.00\",\"2:4\":\"34.00\",\"0:5\":\"120.0\",\"1:5\":\"80.00\",\"2:5\":\"100.0\",\"负其他\":\"34.00\"},\"detail5\":{\"title5\":\"　　总进球数过关固定奖金\",\"0\":\"14.00\",\"1\":\"5.70\",\"2\":\"3.75\",\"3\":\"3.50\",\"4\":\"4.50\",\"5\":\"7.00\",\"6\":\"12.00\",\"7+\":\"16.00\"},\"detail6\":{\"title6\":\"　　半全场胜平负过关固定奖金\",\"胜胜\":\"4.00\",\"胜平\":\"12.00\",\"胜负\":\"23.00\",\"平胜\":\"6.70\",\"平平\":\"6.00\",\"平负\":\"5.10\",\"负胜\":\"26.00\",\"负平\":\"12.00\",\"负负\":\"3.30\"}}";

        System.err.println(getPrizeDetail(str, 1, 2, 3, 4));
    }
}
